//
//  CourseViewController.m
//  MyzcApp
//
//  Created by myzc on 16/6/10.
//  Copyright © 2016年 myzc. All rights reserved.
//

#import "CourseViewController.h"
#import "CourseTableViewCell.h"
#import "AVPViewController.h"
#import "Model.h"
#import "AFHTTPSessionManager.h"
#import "UIKit+AFNetworking.h"
#import "CourseVideoModel.h"
@interface CourseViewController ()<UITableViewDataSource, UITableViewDelegate>

//@property (nonatomic, strong)NSString *ID;
//@property (nonatomic, strong)UITableView *tableview;
@end

@implementation CourseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.title = @"2.请选择课程视频";
     self.navigationController.navigationBarHidden=NO;
    UITableView *tableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] bounds]style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;
    
    [self.view addSubview:tableView];
    
    
    //设置navigationItem上的字体内容
    UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc] init];
    backButtonItem.title = @"";
    self.navigationItem.backBarButtonItem = backButtonItem;

    
    self.courseArray = [NSMutableArray array];
//    [self.courseArray addObject:@"4"];
//    [self.courseArray addObject:@"5"];
//    [self.courseArray addObject:@"6"];
    
    // 创建AFHTTPSessionManager对象
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    //上传json格式
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    
    //声明获取到的数据格式 AFN会json解析返回的数据   有时接口返回的数据不合格会报3840错误，大致是AFN无法解析返回来的数据
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    
    // get请求也可以直接将参数放在字典里，AFN会自己讲参数拼接在url的后面，不需要自己凭借
    //    NSDictionary *param = @{@"user_id":userId, @"sale_date":date, @"accessToken":@"e9c0e60318ebd07ec2fe", @"area_type":areaType};
    // 创建请求类
    
    [manager GET:[NSString stringWithFormat:@"http://114.55.115.175:8080/myzc98Server/courses/%d",_model2.sid] parameters:@{@"id":[NSString stringWithFormat:@"%d",_model2.sid]} progress:^(NSProgress * _Nonnull downloadProgress) {
        // 这里可以获取到目前数据请求的进度
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        // 请求成功
        
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        //             NSLog(@"%@", dict);
        
        //        NSMutableDictionary  *dic = [dict objectForKey:@"data"];
       //        NSMutableArray *jsonArray = [dic objectForKey:@"courseVideoDomainList"];
        
        NSArray *courseVideoDomainList = [[dict objectForKey:@"data"] objectForKey:@"courseVideoDomainList"];
        self.courseArray = [NSMutableArray array];
        
        for (NSDictionary *dic in courseVideoDomainList) {
            
            CourseVideoModel *model = [[CourseVideoModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.courseArray addObject:model];
        }
        
        NSLog(@"dict = %@",dict);
        [tableView reloadData];
        
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        // 请求失败
        
        NSLog(@"%@", [error  localizedDescription]);
        
    }];
    
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier= @"TableViewCell";
    CourseTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[CourseTableViewCell alloc] init];
        if (indexPath.section == 0)
        {
            CourseVideoModel *mo = self.courseArray[indexPath.row];
            cell.textLabel.text = mo.title;
         
        }
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;//cell的右边有一个小箭头，距离右边有十几像素；

    }
    return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.courseArray.count;
}

//-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
//{
//    return self.courseArray.count;
//}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    CourseVideoModel *model  = [_courseArray objectAtIndex:indexPath.row];
    
    AVPViewController *mpVC = [[AVPViewController alloc] init];
    //传值到第三个页面
    mpVC.model = model;
    [self.navigationController pushViewController:mpVC animated:YES];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
